package com.autoglm.data.local

import androidx.room.TypeConverter
import com.autoglm.data.model.ActionStep
import com.autoglm.data.model.ActionType
import com.autoglm.data.model.Point
import com.autoglm.data.model.Rect
import com.autoglm.data.model.StepStatus
import com.autoglm.data.model.TaskStatus
import com.autoglm.data.model.UIElement
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.util.Date

/**
 * Room数据库类型转换器
 */
class Converters {
    
    private val gson = Gson()
    
    @TypeConverter
    fun fromDate(date: Date?): Long? {
        return date?.time
    }
    
    @TypeConverter
    fun toDate(timestamp: Long?): Date? {
        return timestamp?.let { Date(it) }
    }
    
    @TypeConverter
    fun fromTaskStatus(status: TaskStatus): String {
        return status.name
    }
    
    @TypeConverter
    fun toTaskStatus(status: String): TaskStatus {
        return TaskStatus.valueOf(status)
    }
    
    @TypeConverter
    fun fromStepStatus(status: StepStatus): String {
        return status.name
    }
    
    @TypeConverter
    fun toStepStatus(status: String): StepStatus {
        return StepStatus.valueOf(status)
    }
    
    @TypeConverter
    fun fromActionType(type: ActionType): String {
        return type.name
    }
    
    @TypeConverter
    fun toActionType(type: String): ActionType {
        return ActionType.valueOf(type)
    }
    
    @TypeConverter
    fun fromStringList(value: List<String>): String {
        return gson.toJson(value)
    }
    
    @TypeConverter
    fun toStringList(value: String): List<String> {
        val listType = object : TypeToken<List<String>>() {}.type
        return gson.fromJson(value, listType)
    }
    
    @TypeConverter
    fun fromStringMap(value: Map<String, String>): String {
        return gson.toJson(value)
    }
    
    @TypeConverter
    fun toStringMap(value: String): Map<String, String> {
        val mapType = object : TypeToken<Map<String, String>>() {}.type
        return gson.fromJson(value, mapType)
    }
    
    @TypeConverter
    fun fromActionStepList(value: List<ActionStep>): String {
        return gson.toJson(value)
    }
    
    @TypeConverter
    fun toActionStepList(value: String): List<ActionStep> {
        val listType = object : TypeToken<List<ActionStep>>() {}.type
        return gson.fromJson(value, listType)
    }
    
    @TypeConverter
    fun fromUIElement(value: UIElement?): String? {
        return value?.let { gson.toJson(it) }
    }
    
    @TypeConverter
    fun toUIElement(value: String?): UIElement? {
        return value?.let { gson.fromJson(it, UIElement::class.java) }
    }
    
    @TypeConverter
    fun fromPoint(value: Point?): String? {
        return value?.let { gson.toJson(it) }
    }
    
    @TypeConverter
    fun toPoint(value: String?): Point? {
        return value?.let { gson.fromJson(it, Point::class.java) }
    }
    
    @TypeConverter
    fun fromRect(value: Rect?): String? {
        return value?.let { gson.toJson(it) }
    }
    
    @TypeConverter
    fun toRect(value: String?): Rect? {
        return value?.let { gson.fromJson(it, Rect::class.java) }
    }
}
